# -*- coding: utf-8 -*-
# @Date    : 2014-06-19 23:28:16
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

import os
import logging
import inspect
import multiprocesslogging
from conf import settings
# from bootloader

def log_info(handler):
    '''log used in setting's log_function
       for log info
    '''
    log = logging.getLogger(settings.LOG_NAME)
    log.info( '%s %s %s %s' %(handler.get_status(), handler.request.method, handler.request.uri, handler.request.remote_ip))


def log_access(status, method, uri, ip, cost_time,req):
    '''log used in setting's log_function
       for log info
    '''
    log = logging.getLogger(settings.ACCESS_LOG)
    log.info( '%s %s %s %s %s %s' %(status, method, uri, ip, cost_time,req))

def log_api(status, method, uri, ip, cost_time,req, resp):
    '''log used in setting's log_function
       for log info
    '''
    log = logging.getLogger(settings.API_LOG)
    log.info( '%s %s %s %s %s %s %s' %(status, method, uri, ip, cost_time, req, resp))

def log_exception(cls_name, err_info=None):
    '''log used in handler's log_exception
       for log exception
    '''

    func_name = inspect.stack()[1][3]
    log = logging.getLogger(settings.ERR_LOG)
    content = '%s.%s %s' % (cls_name, func_name, err_info)
    log.error('', exc_info=(content , None, None))

def log_sql(sql, *param):
    '''log used in handler's log_sql
       for log sql
    '''
    log = logging.getLogger(settings.SQL_LOG)
    log.info( '%s %s' %(sql,param))
    #TODO to sentry --- queue --- db

def log_warning(hanlder):
    print 'warning'

def init_log(logger_name):
    log = logging.getLogger(logger_name)
    log.setLevel(logging.INFO)

    # formatter = logging.Formatter('%(name)-s %(asctime)s %(levelname)-s %(filename)-s %(message)-s')
    formatter = logging.Formatter('%(asctime)s %(levelname)-s %(message)-s')
    fileHandler = multiprocesslogging.MultiProcessTimedRotatingFileHandler(settings.LOG_PATH + logger_name,'midnight',1,0)
    fileHandler.suffix = "%Y%m%d.log"
    fileHandler.setFormatter(formatter)
    log.addHandler(fileHandler)
    return log